<?php
namespace app\index\controller;

use think\Controller;
use think\Db;

/**
 * 刘家腾
 * 2018-5-30
 * 结算确认详情账单
 */

class Clearing extends Controller
{
	/**
	 * 刘家腾
	 * 2018-5-30
	 * 结算确认详情
	 */
	public function confirm_details()
	{
		$data['code']=666;
		$data['msg']="操作成功";
		$data['data']='';
		$user_info=$this->user;
		$is_authority=self::settlement_authority($user_info);
		if(!$is_authority){
			return ['code'=>30006,'msg'=>"您没有结算权限！",'data'=>''];
			exit;
		}
		$info_batchcode = input('post.info_batchcode');
		if(empty($info_batchcode)){
			return ['code'=>30003,'msg'=>"缺少订单详情号！",'data'=>''];
			exit;
		}
		$info=self::order_details($info_batchcode);//获取订单详情
		if(empty($info)){
			return ['code'=>30003,'msg'=>"订单详情获取失败！",'data'=>''];
			exit;
		}

		$procurement_status=$info['0']['procurement_status'];
		
		$product_info=self::get_product($info['0']['product_id']);//获取产品详情
		
		if($procurement_status==1){//回收单
			$confirm_info['product_id']=$info['0']['product_id'];//产品id
			$confirm_info['product_name']=$info['0']['product_name'];//产品名称
			$confirm_info['cost_price']=$product_info['cost_price'];//采购价（成本价）
			$confirm_info['price']=$product_info['price'];//成交价
			// $confirm_info['']=
			// $confirm_info['']=
			// $confirm_info['']=
			// $confirm_info['']=
			// $confirm_info['']=
			// $confirm_info['']=

		}elseif($procurement_status==2){//寄售单

		}elseif($procurement_status==3){//置换单

		}elseif($procurement_status==4){//预订单（产品未到货/客户只支付定金）

		}
		//测试数据
		$data['orders_info']=$info;
		$data['product_info']=$product_info;

		return $data;
	}

	/**
	 * 刘家腾
	 * 2018-05-31
	 * 获取产品详情
	 * @param      string  $product_id  The product identifier
	 */
	public function get_product($product_id='')
	{
		$product_info= Db::table("product")->alias("p")
				->join("product_other po","p.id=po.product_id","left")
				->where('p.id',$product_id)
				->where('p.state',0)
				->group('description')
				->select();
		$product_pic= Db::table('productpic')->where("product_id",$product_id)->select();
		$product_data=array();
		if(!empty($product_info)){
			$product_data=$product_info['0'];
			if (!empty($product_pic)) {
				$product_data['product_pic']=$product_pic;
			}
		}
		return $product_data;

	}
	/**
	 * 刘家腾
	 * 2018-5-31
	 * 查询订单详情
	 * @param      string  $info_batchcode  The value
	 */
	public function order_details($info_batchcode='')
	{
		return Db::table("orders_info")->where('info_batchcode',$info_batchcode)->select();
	}

	/**
	 * 刘家腾
	 * 2018-5-31
	 * 判断登录账号是否有结算权限
	 * @param      string  $user_info  The value
	 */
	public function settlement_authority($user_info='')
	{
		if($user_info['is_business']==1){
			$code=true;
		}elseif($user_info['shop']==1){
			$code=true;
		}elseif($user_info['is_staff']==1){//position_id
		    $map['s.user_id']=$user_info['id'];
		    $map['h.type']=['>=',99];
			$stafflist=Db::table("staff")->alias("s")
						->join('haracter h','s.position_id = h.id',"LEFT")
                        ->where($map)
                        ->order('s.id desc')
                        ->count();
            // echo Db::table('staff')->getLastSql();
            
            $code=$stafflist;
		}else{
			$code=false;
		}
		return $code;
	}

}